package com.example.springbootdemo.controller;

import com.example.springbootdemo.dto.UserLoginDTO;
import com.example.springbootdemo.pojo.UserLogin;
import com.example.springbootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {
    @Autowired
    UserService userService;

    @RequestMapping("{testGetStaticSource}/{index}")
    public String getStaticSoutce(@PathVariable String testGetStaticSource, @PathVariable String index){
        return testGetStaticSource+"/"+index; //依据path中的路径组合返回页面
    }

    @GetMapping(value = "login")
    public String login() {
        return "login";
    }

    @GetMapping(value = "loginIn")
    public String loginGet(String userAccount,String userPwd){
        UserLogin user = userService.loginCheck(userAccount,userPwd);
        if (user!=null) {
            return "success";
        } else {
            return "failed";
        }
    }

    @PostMapping(value = "loginIn")
    public String loginPost(@RequestBody UserLoginDTO userLoginDTO) { //注意这里的注解，不能少！
        UserLogin userLogin;
        userLogin = userService.loginCheck(userLoginDTO.getUserNo());
        if (userLogin!=null) {
            return "success";
        }

        userLogin = userService.loginCheck(userLoginDTO.getUserAccount(),userLoginDTO.getUserPwd());
        if (userLogin!=null) {
            return "success";
        } else {
            return "failed";
        }
    }
}
